﻿using System;
using System.Globalization;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;

namespace MonkeyWarranty.Web.Customers
{
    public partial class Edit : System.Web.UI.Page
    {
        #region Properties

        private CustomerBiz _customerBiz;
        private CustomerBiz CustomerManager
        {
            get { return _customerBiz ?? (_customerBiz = new CustomerBiz()); }
        }

        private int _customerId;
        private int CustomerId
        {
            get
            {
                if (_customerId <= 0)
                    int.TryParse(Request.QueryString["Id"], out _customerId);
                return _customerId;
            }
        }

        private Customer _customer;
        private Customer CurrentCustomer
        {
            get
            {
                if (_customer == null && CustomerId > 0)
                {
                    _customer = CustomerManager.GetById(CustomerId);
                }
                return _customer;
            }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Load data
                // load customer groups
                var groups = CustomerManager.GetAllCustomerGroups();//_repository.CityRepository.GetAllCustomerGroups();
                groups.Insert(0, new CustomerGroup { Id = 0 });
                ddlCustomerGroups.DataTextField = "Name";
                ddlCustomerGroups.DataValueField = "Id";
                ddlCustomerGroups.DataSource = groups;
                ddlCustomerGroups.DataBind();
                
                // Load customer data
                if (CurrentCustomer == null)
                {
                    lblErrorMessage.Text = string.Format("{0}: Id={1}", Resources.GlobalStrings.NotExisted, CustomerId);
                    btnSave.Enabled = false;
                }
                else
                {
                    ddlCustomerGroups.SelectedValue = CurrentCustomer.CustomerGroupId.ToString();
                    txtCode.Text = CurrentCustomer.Code;
                    txtName.Text = CurrentCustomer.Name;
                    txtAddress.Text = CurrentCustomer.Address;
                    txtPhone.Text = CurrentCustomer.Phone;
                    txtFax.Text = CurrentCustomer.Fax;
                    txtEmail.Text = CurrentCustomer.Email;
                    txtWebsite.Text = CurrentCustomer.Website;
                    txtTaxCode.Text = CurrentCustomer.TaxCode;
                    txtBankAccount.Text = CurrentCustomer.BankAccount;
                    txtBankName.Text = CurrentCustomer.BankName;
                    txtBankProvince.Text = CurrentCustomer.BankProvince;
                    txtDirectorName.Text = CurrentCustomer.DirectorName;
                    txtAccounterName.Text = CurrentCustomer.AccounterName;
                    txtDescription.Text = CurrentCustomer.Description;
                    txtSortOrder.Text = CurrentCustomer.SortOrder.ToString(CultureInfo.InvariantCulture);
                    chkHidden.Checked = CurrentCustomer.Hidden;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;
            // validate inputs
            var code = txtCode.Text.Trim();
            if (code.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.CodeIsRequired;
                return;
            }
            var name = txtName.Text.Trim();
            if (name.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.NameIsRequired;
                return;
            }
            var descrition = txtDescription.Text;
            int sortOrder;
            int.TryParse(txtSortOrder.Text.Trim(), out sortOrder);
            var isHidden = chkHidden.Checked;
            var address = txtAddress.Text.Trim();
            var phone = txtPhone.Text.Trim();
            var fax = txtFax.Text.Trim();
            var email = txtEmail.Text.Trim();
            var website = txtWebsite.Text.Trim();
            var taxcode = txtTaxCode.Text.Trim();
            var bankAccount = txtBankAccount.Text.Trim();
            var bankName = txtBankName.Text.Trim();
            var bankProvince = txtBankProvince.Text.Trim();
            var director = txtDirectorName.Text.Trim();
            var accounter = txtAccounterName.Text.Trim();
            int customerGroupId;
            int.TryParse(ddlCustomerGroups.SelectedValue, out customerGroupId);

            // get data
            CurrentCustomer.Code = code;
            CurrentCustomer.Name = name;
            CurrentCustomer.Address = address;
            CurrentCustomer.Phone = phone;
            CurrentCustomer.Fax = fax;
            CurrentCustomer.Email = email;
            CurrentCustomer.Website = website;
            CurrentCustomer.TaxCode = taxcode;
            CurrentCustomer.BankAccount = bankAccount;
            CurrentCustomer.BankName = bankName;
            CurrentCustomer.BankProvince = bankProvince;
            CurrentCustomer.DirectorName = director;
            CurrentCustomer.AccounterName = accounter;
            CurrentCustomer.Description = descrition;
            CurrentCustomer.SortOrder = sortOrder;
            CurrentCustomer.Hidden = isHidden;
            CurrentCustomer.CustomerGroupId = customerGroupId;

            // save data
            CustomerManager.UpdateCustomer(CurrentCustomer);

            // return
            Response.Redirect("List.aspx");
        }

        #endregion
    }
}